define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

  var Controller = {
    index: function () {
      // 初始化表格参数配置
      Table.api.init({
        extend: {
          index_url: 'jy/user_access_log/index' + location.search,
          add_url: '',
          edit_url: '',
          del_url: '',
          multi_url: 'jy/user_access_log/multi',
          import_url: 'jy/user_access_log/import',
          table: 'user_access_log',
        }
      });

      var table = $("#table");

      // 初始化表格
      table.bootstrapTable({
        url: $.fn.bootstrapTable.defaults.extend.index_url,
        pk: 'id',
        sortName: 'ua.update_time',
        search: false,
        undefinedText: '',
        exportOptions: {
          fileName: 'export_' + Moment().format("YYYY-MM-DD"),
          ignoreColumn: ['operate'] //默认不导出第一列(checkbox)与操作(operate)列
        },
        queryParams: function (params) {
          var filter = JSON.parse(params.filter);
          var op = JSON.parse(params.op);
          var sort = params.sort;
          if (sort === 'update_time') {
            params.sort = 'ua.update_time';
          }
          if (filter['user_id']) {
            filter['ua.user_id'] = filter['user_id'];
            op['ua.user_id'] = op['user_id'];
            delete filter['user_id'];
            delete op['user_id'];
          }
          if (filter['nickname']) {
            filter['u.nickname'] = filter['nickname'];
            op['u.nickname'] = op['nickname'];
            delete filter['nickname'];
            delete op['nickname'];
          }
          if (filter['invite_type']) {
            filter['ui.invite_type'] = filter['invite_type'];
            op['ui.invite_type'] = op['invite_type'];
            delete filter['invite_type'];
            delete op['invite_type'];
          }
          if (filter['invite_nickname']) {
            filter['u1.nickname'] = filter['invite_nickname'];
            op['u1.nickname'] = op['invite_nickname'];
            delete filter['invite_nickname'];
            delete op['invite_nickname'];
          }
          if (filter.update_time) {
            filter['ua.update_time'] = filter.update_time;
            op['ua.update_time'] = "RANGE";
            delete filter['update_time'];
            delete op['update_time'];
          }
          params.filter = JSON.stringify(filter);
          params.op = JSON.stringify(op);
          return params;
        },
        columns: [
          [
            {field: 'id', title: __('Id'), operate: false},
            {field: 'user_id', title: __('User_id'), visible: false},
            {
              field: 'avatar',
              title: __('User.avatar'),
              operate: false,
              events: Table.api.events.image,
              formatter: Table.api.formatter.image
            },
            {field: 'nickname', title: __('User.nickname'), operate: 'LIKE'},
            {
              field: 'update_time',
              title: __('Update_time'),
              operate: 'RANGE',
              addclass: 'datetimerange',
              autocomplete: false
            },
            {
              field: 'invite_type',
              title: __('Userinvite.invite_type'),
              searchList: {"1": __('Invite_type 1'), "2": __('Invite_type 2'), "3": __('Invite_type 3')},
              formatter: Table.api.formatter.normal
            },
            {
              field: 'invite_avatar',
              title: __('Invitor.avatar'),
              operate: false,
              events: Table.api.events.image,
              formatter: Table.api.formatter.image
            },
            {field: 'invite_nickname', title: __('Invitor.nickname'), operate: 'LIKE'},
          ]
        ]
      });

      // 为表格绑定事件
      Table.api.bindevent(table);
    },
    add: function () {
      Controller.api.bindevent();
    },
    edit: function () {
      Controller.api.bindevent();
    },
    api: {
      bindevent: function () {
        Form.api.bindevent($("form[role=form]"));
      }
    }
  };
  return Controller;
});